package entity

import (
	"errors"
	"fmt"

	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

//COMP_GSZ 托管中收导入
type COMP_GSZ struct {
	ID     int     `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	NY     int     `gorm:"column:NY"`
	CUID   int     `gorm:"column:CUID"`
	ZJJC   string  `gorm:"column:ZJJC"`
	KHMC   string  `gorm:"column:KHMC"`
	LR     float32 `gorm:"column:LR"`
	XHCCJE float32 `gorm:"column:XHCCJE"`
	JJZB   float32 `gorm:"column:JJZB"`
	DRJG   string  `gorm:"column:DRJG"`

	EMID1 int     `gorm:"column:EMID1"`
	EMNO1 string  `gorm:"column:EMNO1"`
	ZB1   float32 `gorm:"column:ZB1"`
	EMID2 int     `gorm:"column:EMID2"`
	EMNO2 string  `gorm:"column:EMNO2"`
	ZB2   float32 `gorm:"column:ZB2"`
	EMID3 int     `gorm:"column:EMID3"`
	EMNO3 string  `gorm:"column:EMNO3"`
	ZB3   float32 `gorm:"column:ZB3"`
	EMID4 int     `gorm:"column:EMID4"`
	EMNO4 string  `gorm:"column:EMNO4"`
	ZB4   float32 `gorm:"column:ZB4"`
	EMID5 int     `gorm:"column:EMID5"`
	EMNO5 string  `gorm:"column:EMNO5"`
	ZB5   float32 `gorm:"column:ZB5"`
}

//TableName 经营单位中收导入表
func (COMP_GSZ) TableName() string {
	return "T_COMP_GSZ_DTIP"
}

//BeforeSave 保存前的触发器
func (i *COMP_GSZ) BeforeSave(tx *gorm.DB) (err error) {
	if i.EMNO1 != "" {
		sum := i.ZB1 + i.ZB2 + i.ZB3 + i.ZB4 + i.ZB5
		if sum != 100 {
			return errors.New("占比之和必须为100！")
		}
		var emid1, emid2, emid3, emid4, emid5 int
		var emnm1, emnm2, emnm3, emnm4, emnm5 string
		sqlEx := "select ID,SysUserName from T_SysUser where SysUserID=? or SysUserName=?"
		row := tx.Raw(sqlEx, i.EMNO1, i.EMNO1).Row()
		row.Scan(&emid1, &emnm1)
		if emid1 == 0 && emnm1 != "分行公共" {
			return fmt.Errorf("员工号1:%s在系统中没有找到！", i.EMNO1)
		}
		i.EMID1 = emid1
		if i.ZB2 > 0 {
			row = tx.Raw(sqlEx, i.EMNO2, i.EMNO2).Row()
			row.Scan(&emid2, &emnm2)
			if emid2 == 0 && emnm2 != "分行公共" {
				return fmt.Errorf("员工号2:%s在系统中没有找到！", i.EMNO2)
			}
			i.EMID2 = emid2
		}
		if i.ZB3 > 0 {
			row = tx.Raw(sqlEx, i.EMNO3, i.EMNO3).Row()
			row.Scan(&emid3, &emnm3)
			if emid3 == 0 && emnm3 != "分行公共" {
				return fmt.Errorf("员工号3:%s在系统中没有找到！", i.EMNO2)
			}
			i.EMID3 = emid3
		}
		if i.ZB4 > 0 {
			row = tx.Raw(sqlEx, i.EMNO4, i.EMNO4).Row()
			row.Scan(&emid4, &emnm4)
			if emid4 == 0 && emnm4 != "分行公共" {
				return fmt.Errorf("员工号4:%s在系统中没有找到！", i.EMNO4)
			}
			i.EMID4 = emid4
		}
		if i.ZB5 > 0 {
			row = tx.Raw(sqlEx, i.EMNO5, i.EMNO5).Row()
			row.Scan(&emid5, &emnm5)
			if emid5 == 0 && emnm5 != "分行公共" {
				return fmt.Errorf("员工号5:%s在系统中没有找到！", i.EMNO5)
			}
			i.EMID5 = emid5
		}
	}

	return
}

//COMP_GSZAfterImport 导入后执行
func COMP_GSZAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	prams := make([]interface{}, 1)
	prams[0] = params["NY"]
	tx.Exec("EXEC UP_COMP_GSZ_DTIP ?", prams...)
	return nil
}

//COMP_GSZEF 返回结构体
func COMP_GSZEF() base.EntityFunc {
	return func() interface{} {
		entity := COMP_GSZ{}
		return &entity
	}
}

//COMP_GSZESF 返回结构体数组
func COMP_GSZESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []COMP_GSZ
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("COMP_GSZ", base.NewDefaultDao(COMP_GSZEF(), COMP_GSZESF(), nil))
	base.SetAfterCallBack("COMP_GSZ", COMP_GSZAfterImport)
}
